from bs4 import BeautifulSoup
import requests
import lxml
import bs4
from fake_useragent import UserAgent


url = "http://localhost/shopxo/index.php?s=/index/search/goodslist.html"
ua = UserAgent()
try:
    headers = {
        'User-Agent': ua.random,
        "X-Requested-With": "XMLHttpRequest"
    }
    data = {
        "category_id": 0,
        "wd": "手机",
        "page": 1,
        "order_by_field": "default",
        "order_by_type": "asc"
    }
    response = requests.post(url,data=data,headers=headers)
except:
    raise Exception('请求失败')

result = response.json()
html_content = result["data"]["data"]

soup = BeautifulSoup(html_content,'lxml')
li = soup.select('.am-animation-scale-up')
list_phone = []
dict_phone = {}
for item in li:
    dict_phone['title'] = item.find('p',{'class':'goods-title'}).string
    dict_phone['price'] = item.select_one('.price strong').string
    dict_phone['sale_count'] = item.find('span',{'class':'sales-count'}).string
    list_phone.append(dict_phone)
print(list_phone)